/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.serialization;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import net.miblounge.gradecalculator.core.student.ExaminationResultType;


public final class GradeAdapter extends XmlAdapter<GradeList, Map<ExaminationResultType, String>> {

	@Override
	public Map<ExaminationResultType, String> unmarshal(final GradeList v)
			throws Exception {
		final HashMap<ExaminationResultType, String> result = new HashMap<ExaminationResultType, String>();
		
		for (GradeEntry entry : v.getList()) {
			result.put(ExaminationResultType.valueOf(entry.type), entry.grade);
		}
		
		return result;
	}

	@Override
	public GradeList marshal(final Map<ExaminationResultType, String> v)
			throws Exception {
		final GradeList result = new GradeList();
		for (ExaminationResultType key : v.keySet()) {
			final GradeEntry entry = new GradeEntry();
			entry.type = key.getName();
			entry.grade = v.get(key);

			result.getList().add(entry);
		}

		return result;
	}
	
	
}